<!--
 * @Author: C.
 * @Date: 2021-09-03 13:21:03
 * @LastEditTime: 2021-09-06 14:21:27
 * @Description: file content
-->
<template>
  <BasicModal
    @register="register"
    :title="title"
    :helpMessage="['提示1', '提示2']"
    @ok="handleSubmit"
    :minHeight="100"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, onMounted } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { FieldSchema } from '../../../form.config';

  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      title: {
        type: String,
        default: '表格标题',
      },
      formSchema: {
        type: Array,
        default: FieldSchema,
      },
      defaultData: {
        type: Object,
        default: () => {},
      },
    },
    emits: ['success'],
    setup(props, { emit }) {
      const [register, { closeModal, setModalProps }] = useModalInner();
      const [registerForm, { resetFields, validate, setFieldsValue }] = useForm({
        labelWidth: 100,
        layout: 'horizontal',
        schemas: props.formSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 23,
        },
      });
      async function handleSubmit() {
        try {
          const values = await validate();
          setModalProps({ confirmLoading: true });
          // TODO custom api
          console.log(values);
          closeModal();
          emit('success', values);
          resetFields();
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }
      return {
        register,
        registerForm,
        handleSubmit,
      };
    },
  });
</script>
